package com.tengju.bff.interfaces.manage.star;

import com.tengju.bff.interfaces.manage.star.facade.SignFacade;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.user.application.archives.StarContractService;
import com.tengju.user.domain.model.user.UserIdCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.OutputStream;

@Validated
@RestController
@Api(tags = "星级签约合同管理")
@RequestMapping(value = "manage/star/contract")
@Slf4j
@ManageEntrance
public class StarContractController {

    @Autowired
    StarContractService starContractService;

    @Autowired
    SignFacade signFacade;


    @Value("${star.contract.filename}")
    private String contractFilename;

    @RequiresRoles("sso")
    @ApiOperation(value = "dt后台星级用户下载合同", notes = "dt后台星级用户下载合同")
    @ApiImplicitParam(name = "idCode", value = "星级黑卡idCode", required = true, dataType = "Long")
    @RequestMapping(value = "getStarAgreement", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void getStarAgreement(@NotNull @Min(value = 1) @RequestParam(value = "idCode") Long idCode,
                                 @NotNull @Min(value = 1) @RequestParam(value = "starRole") Integer starRole,
                                 HttpServletRequest request, HttpServletResponse response) throws Exception {
        //byte[] buffer = starContractService.getStarAgreementByte(new UserIdCode(idCode), starRole);


        byte[] buffer = signFacade.downloadContract(new UserIdCode(idCode), starRole);

//        BufferedOutputStream bos = null;
//        FileOutputStream fos = null;
//        File file = null;
//        File dir = new File("E:\\access\\");
//        if (!dir.exists() && dir.isDirectory()) {
//            dir.mkdirs();
//        }
//        file = new File("E:\\access\\签约.pdf");
//        fos = new FileOutputStream(file);
//        bos = new BufferedOutputStream(fos);
//        bos.write(buffer);

        OutputStream outputStream = null;
        try {
            if (null != buffer) {
                outputStream = response.getOutputStream();
                outputStream.write(buffer);
                outputStream.flush();
            }
        } catch (Exception e) {
            log.error("合同下载出现异常, idCode:{}, 异常信息为:", idCode, e);
        } finally {
            if (null != outputStream) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    log.error("合同下载关闭输出流异常, idCode:{},", idCode, e);
                }
            }
        }
    }
}
